<template>
	<view class="content">
		<view class="nav">
			<uni-segmented-control :current="0" :values="titles" @clickItem="onClickItem" styleType="button" activeColor="#4cd964"></uni-segmented-control>
		</view>
		<view class="layout">
			<view class="box" v-for="(item,index) in arrs" :key="item._id">
				<view class="pic">
					<image lazy-load :src="item.url" mode="widthFix" @click="preView(index)"></image>
				</view>
				<view class="text">{{item.content}}</view>
				<view class="author">--{{item.author}}</view>
			</view>
		</view>
		<view class="float">
			<view class="refresh" @click="refresh">
				<uni-icons type="refreshempty" size="30"></uni-icons>
			</view>
			<view class="top" @click="top">
				<uni-icons type="arrow-up" size="30"></uni-icons>
			</view>
		</view>
	</view>

</template>

<script setup>
	import {computed, ref} from 'vue'
	import {onReachBottom,onPullDownRefresh} from '@dcloudio/uni-app'
	const arrs = ref([]);
	
	const current = ref(0);
	const classList = [{type:'all',title:'全部'},{type:'dog',title:'狗狗'},{type:'cat',title:'猫咪'}];
	
	// const titles = ['全部','狗狗','猫咪'];
	const titles = computed(()=>classList.map(item=>item.title));
	//刷新
	const refresh = function(){
		arrs.value = []; //清空原来的数据
		getData();
	}
	//置顶
	const top = function(){
		uni.pageScrollTo({
			scrollTop:0,
			duration:100
		})
	}
	
	//选项卡触发函数
	const onClickItem = function(e){
		current.value = e.currentIndex;
		console.log(e);
		arrs.value = [];  //先清空数据
		getData();
	}
	//图片预览
	const preView = function(index){
		let urls = arrs.value.map(item => item.url);
		uni.previewImage({
			current:index,  //图片下标
			urls: urls
		})
	}
	
	//获取数据
	function getData(){
		uni.showNavigationBarLoading();
		uni.request({
			url:'https://tea.qingnian8.com/tools/petShow',
			data:{
				size:5,
				type:classList[current.value].type
			},
			header:{
				'access-key':"260154"
			}
		}).then((res)=>{			
			if(res.data.errCode === 0){
				//成功获取
				// arrs.value = arrs.value.concat(res.data.data);
				arrs.value = [ ...arrs.value,...res.data.data ]
				console.log(arrs.value);
			}else if(res.data.errCode === 400){
				
			}
		}).catch(err=>{
			
		}).finally(()=>{
			uni.stopPullDownRefresh();
			uni.hideNavigationBarLoading();
		})
	}
	getData();
	//触底加载更多
	onReachBottom(()=>{
		getData();
	})
	//下拉刷新
	onPullDownRefresh(()=>{
		arrs.value = []; //清空原来的数据
		getData();
	})
</script>

<style lang="scss">
	.content {
		.nav{
			padding: 50rpx 50rpx 0;
		}
		.layout{
			padding: 50rpx;
			.box{
				margin-bottom: 60rpx;
				border-radius: 20rpx;
				box-shadow: 0 10rpx 30rpx rgba(0,0,0,0.09);
				.pic{
					image{
						width: 100%;
					}
				}
				.text{
					padding: 30rpx 0 30rpx 0;
					font-size: 36rpx;
					color: #969696;
				}
				.author{
					padding-bottom: 30rpx;
					padding-right: 10rpx;
					text-align: right;
					font-size: 24rpx;
					color:#bebebe;
				}
			}
		}
		.float{
			position: fixed;
			right: 20rpx;
			bottom: 50rpx;
			.refresh,.top{
				width: 100rpx;
				height: 100rpx;
				background: rgba(255,255,255,0.6);
				border-radius: 50%;
				display: flex;
				justify-content: center;
				align-items: center;
				margin-bottom: 10rpx;
			}
		}
	}

	
</style>
